package com.fh.ms.service.impl;

import com.fh.common.bo.Order;
import com.fh.ms.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @FileName : OrderServiceImpl
 * @Description :
 * @Author : fh
 * @Date : 2020/11/19 11:36
 * @Version : 1.0.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    @HystrixCommand(fallbackMethod = "fallBackQueryAll")
    public List<Order> queryAll() {
        return restTemplate.postForObject("http://orderService/order/queryAll",
                new Order(),List.class);
    }

    /**
     * 备份方法，远程服务一旦宕机，该方法起效 要求返回值和参数都要与原方法一致
     * @return
     */
    private List<Order> fallBackQueryAll(){
        List<Order> orders = new ArrayList<>();
        Order order=new Order();
        order.setId(44444);
        order.setMemberUsername("远程服务器宕机，该下课了。。。");
        orders.add(order);
        return orders;
    }

    @Override
    public Order queryById(Integer id) {
        return restTemplate.getForObject("http://orderService/order/selectOne?id="+id,
                Order.class,String.class);
    }
}
